﻿namespace ContactsSync.Providers.Tests.ViewModels
{
    using System.Collections.ObjectModel;
    using Infrastructure.Models;
    using Microsoft.Practices.Composite.Events;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;
    using Providers.ViewModels;

    [TestClass]
    public class BaseProviderViewModelFixture
    {
        [TestMethod]
        public void ShouldRaisePropertyChangedEventWhenContactsChange()
        {
            bool propertyChangedRaised = false;
            string propertyName = null;

            var eventAggregator = new Mock<IEventAggregator>();

            var viewModel = new TestableBaseProviderViewModel(eventAggregator.Object);

            viewModel.PropertyChanged += (sender, e) =>
                                             {
                                                 propertyChangedRaised = true;
                                                 propertyName = e.PropertyName;
                                             };

            viewModel.BaseSetContacts(new ObservableCollection<Contact>());

            Assert.IsTrue(propertyChangedRaised);
            Assert.AreEqual("Contacts", propertyName);
        }

        private class TestableBaseProviderViewModel : BaseProviderViewModel
        {
            public TestableBaseProviderViewModel(IEventAggregator eventAggregator) : base(eventAggregator)
            {
            }

            public void BaseSetContacts(ObservableCollection<Contact> contacts)
            {
                this.Contacts = contacts;
            }
        }
    }
}
